{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "370cc844-afa5-406c-9ba9-7610af811c91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.4.0+cpu\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(torch.__version__)  # 应输出类似2.13.0\n",
    "print(torch.cuda.is_available())  # 检查GPU支持 True支持 False不支持"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cab4c66f-e4af-4c41-8d56-837af76fa793",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建未初始化张量\n",
    "x = torch.empty(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4d19e4af-9119-4130-90aa-c7d00a972a75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-5.9641e-08,  1.7012e-42,  0.0000e+00],\n",
       "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3cce2794-dff3-4ff0-959e-3019f0af7c16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建随机张量（0-1均匀分布）\n",
    "rand_tensor = torch.rand(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5c724c13-a205-448a-ba07-1b3174fd8d2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[5.3426e-02, 4.6318e-01, 9.3043e-05],\n",
       "        [1.4021e-01, 3.8874e-01, 1.0472e-01]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8956fe3a-2a62-4fd3-802d-ddfd428c8956",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建全零张量（指定类型）\n",
    "zero_tensor = torch.zeros(4, dtype=torch.long)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "382d12b3-db79-486c-8291-7c2d1e10ef53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 0, 0, 0])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zero_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d75e4fc4-0a7e-4acf-bf1e-437399abb96f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从数据直接创建\n",
    "data_tensor = torch.tensor([5.5, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "942276a3-8b4b-412a-8a0f-ae5943ef74f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([5.5000, 3.0000])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4718818e-8665-4909-8dd8-823058e45b63",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.rand(2, 2)\n",
    "b = torch.rand(2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2b282056-fbe4-4cec-98b7-c4b62118536d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0.4882, 0.7736],\n",
       "         [0.5019, 0.2525]]),\n",
       " tensor([[0.8902, 0.8300],\n",
       "         [0.5279, 0.0966]]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e833ee05-ae64-41f4-bbfb-df44bd7a5d68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2.2686, 2.4335],\n",
       "        [1.5577, 0.4456]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加法三种写法\n",
    "c1 = a + b\n",
    "c2 = torch.add(a, b)\n",
    "a.add_(b)  # 原位操作 直接打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "954fb4db-f3ae-4e08-a321-f57a987848e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[1.3784, 1.6035],\n",
       "         [1.0298, 0.3491]]),\n",
       " tensor([[1.3784, 1.6035],\n",
       "         [1.0298, 0.3491]]))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1,c2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0aed195a-63ef-43be-91fd-282b7351b9b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵乘法\n",
    "matmul = torch.mm(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c677afbc-318b-4b14-b1d0-f81087ede210",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2.0735, 1.2988],\n",
       "        [1.1010, 0.8884]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matmul"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1b06067c-f4da-4f73-8070-c3121bf6e660",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 索引切片\n",
    "row = a[1, :]  # 第二行\n",
    "col = a[:, 0]  # 第一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "559b53fc-95e6-4578-aa4f-a7c369066d62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.0298, 0.3491])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "19739026-1835-44d6-84b6-46498e4e4869",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.3784, 1.0298])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9c65ef14-f47b-4267-be41-c281363d620e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 与Numpy 转换\n",
    "import numpy as np\n",
    "\n",
    "# Tensor -> Numpy\n",
    "t = torch.ones(5)\n",
    "n = t.numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "078b4f44-3f7a-47bc-8377-586f7bc4ecce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([1., 1., 1., 1., 1.]), array([1., 1., 1., 1., 1.], dtype=float32))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t,n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "61f4ec50-1c17-4137-81be-9cbdb7746e36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Numpy -> Tensor\n",
    "n = np.ones(5)\n",
    "t = torch.from_numpy(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f8b0a4bf-4413-4e77-be7f-515be0842f6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 1., 1., 1., 1.]),\n",
       " tensor([1., 1., 1., 1., 1.], dtype=torch.float64))"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n,t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "3c1adefb-4712-426c-87e2-85b428eef829",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GPU张量需显式指定设备\n",
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "gpu_tensor = torch.rand(2,2).to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a1892e68-b2fb-472f-b3ad-b728f3d89af9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('cpu',\n",
       " tensor([[0.7150, 0.8111],\n",
       "         [0.6600, 0.1519]]))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "device,gpu_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b730180a-dce4-400b-94d6-0211570bff09",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 张量维度需匹配时使用view()调整形状\n",
    "x = torch.arange(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4b2031e0-8a8a-499c-8dbb-1a2acca9d491",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cd5094cd-115e-43fc-a8ac-2d8361d2ed9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = x.view(2, 3)  # 形状改为2x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f6a2a32f-501a-4103-ba34-ecbdcba35f16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2],\n",
       "        [3, 4, 5]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "296559d5-2c3b-443a-8142-2d28274244a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32ed1739-6dc8-4798-8417-8a6a456d5a6a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
